﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Common.Constants;
using DataAccess;
using Entities;

namespace Business
{
    /// <summary>
    /// 
    /// </summary>
    public class FacilityBusiness
    {

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static FacilityBusiness Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="FacilityBusiness" /> class.
        /// </summary>
        static FacilityBusiness()
        {
            Instance = new FacilityBusiness();
        }

        /// <summary>
        /// Lists the facility.
        /// </summary>
        /// <param name="premiseId">The premise id.</param>
        /// <param name="keyFilter">The key filter.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <returns></returns>
        public Collection<Facility> ListFacility(string premiseId, string keyFilter, bool isActive)
        {
            const string digitPattern = "^[0-9]";
            const string aePattern = "^[a-eA-E]";
            const string fjPattern = "^[f-jF-J]";
            const string knPattern = "^[k-nK-N]";
            const string orPattern = "^[o-rO-R]";
            const string svPattern = "^[s-vS-V]";
            const string wzPattern = "^[w-zW-Z]";
            Collection<Facility> lstOrganisation;
            try
            {
                lstOrganisation = FacilityDao.Instance.ListFacilityByPremise(premiseId, isActive);
            }
            catch (SqlException)
            {
                lstOrganisation = null;
            }
            if (lstOrganisation != null)
                switch (keyFilter)
                {
                    case "All":
                        return lstOrganisation;
                    case "09":
                        return Filter(digitPattern, lstOrganisation);
                    case "ae":
                        return Filter(aePattern, lstOrganisation);
                    case "fj":
                        return Filter(fjPattern, lstOrganisation);
                    case "kn":
                        return Filter(knPattern, lstOrganisation);
                    case "or":
                        return Filter(orPattern, lstOrganisation);
                    case "sv":
                        return Filter(svPattern, lstOrganisation);
                    case "wz":
                        return Filter(wzPattern, lstOrganisation);
                }
            return null;
        }

        /// <summary>
        /// Filters the specified pattern.
        /// </summary>
        /// <param name="pattern">The pattern.</param>
        /// <param name="lstOrg">The LST org.</param>
        /// <returns></returns>
        private Collection<Facility> Filter(string pattern, Collection<Facility> lstOrg)
        {
            var query = from o in lstOrg
                        where checkPattern(pattern, o.EquipmentName)
                        select o;
            return new Collection<Facility>(query.ToList());
        }

        /// <summary>
        /// Checks the pattern.
        /// </summary>
        /// <param name="pattern">The pattern.</param>
        /// <param name="target">The target.</param>
        /// <returns></returns>
        private bool checkPattern(string pattern, string target)
        {
            var myRegex = new Regex(pattern);
            var match = myRegex.Match(target);
            return match.Success;
        }

        /// <summary>
        /// Pagings the org.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="facilities">The facilities.</param>
        /// <returns></returns>
        public Collection<Facility> PagingOrg(int pageNumber, int pageSize, Collection<Facility> facilities)
        {
            if (facilities != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = facilities.Count % pageSize == 0
                                        ? facilities.Count / pageSize
                                        : facilities.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return
                            new Collection<Facility>(
                                facilities.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());
                }
                return null;
            }
            return null;
        }

        /// <summary>
        /// Marks the inactive.
        /// </summary>
        /// <param name="facilityId">The facility id.</param>
        /// <returns></returns>
        public bool MarkInactive(Guid facilityId)
        {
            try
            {
                return FacilityDao.Instance.MarkInactive(facilityId);
            }
            catch (SqlException)
            {
                return false;
            }
        }

        /// <summary>
        /// Orders the by lead contact by premise.
        /// </summary>
        /// <param name="premise">The premise.</param>
        /// <param name="keyFilter">The key filter.</param>
        /// <param name="orderMode">if set to <c>true</c> [order mode].</param>
        /// <returns></returns>
        public Collection<Facility> OrderByLeadContactByPremise(string premise, string keyFilter, bool orderMode)
        {
            const string digitPattern = "^[0-9]";
            const string aePattern = "^[a-eA-E]";
            const string fjPattern = "^[f-jF-J]";
            const string knPattern = "^[k-nK-N]";
            const string orPattern = "^[o-rO-R]";
            const string svPattern = "^[s-vS-V]";
            const string wzPattern = "^[w-zW-Z]";
            Collection<Facility> lstOrganisation;
            try
            {
                lstOrganisation = FacilityDao.Instance.OrderByLeadContactByPremise(premise, orderMode);
            }
            catch (SqlException)
            {

                lstOrganisation = null;
            }

            if (lstOrganisation != null)
                switch (keyFilter)
                {
                    case "All":
                        return lstOrganisation;
                    case "09":
                        return Filter(digitPattern, lstOrganisation);
                    case "ae":
                        return Filter(aePattern, lstOrganisation);
                    case "fj":
                        return Filter(fjPattern, lstOrganisation);
                    case "kn":
                        return Filter(knPattern, lstOrganisation);
                    case "or":
                        return Filter(orPattern, lstOrganisation);
                    case "sv":
                        return Filter(svPattern, lstOrganisation);
                    case "wz":
                        return Filter(wzPattern, lstOrganisation);
                }
            return null;
        }

        /// <summary>
        /// Orders the by room host by premise.
        /// </summary>
        /// <param name="keyFilter">The key filter.</param>
        /// <param name="premise">The premise.</param>
        /// <param name="orderMode">if set to <c>true</c> [order mode].</param>
        /// <returns></returns>
        public Collection<Facility> OrderByRoomHostByPremise(string keyFilter, string premise, bool orderMode)
        {
            const string digitPattern = "^[0-9]";
            const string aePattern = "^[a-eA-E]";
            const string fjPattern = "^[f-jF-J]";
            const string knPattern = "^[k-nK-N]";
            const string orPattern = "^[o-rO-R]";
            const string svPattern = "^[s-vS-V]";
            const string wzPattern = "^[w-zW-Z]";
            Collection<Facility> lstOrganisation;
            try
            {
                lstOrganisation = FacilityDao.Instance.OrderByRoomHostByPremise(premise, orderMode);
            }
            catch (SqlException)
            {
                lstOrganisation = null;
            }

            if (lstOrganisation != null)
                switch (keyFilter)
                {
                    case "All":
                        return lstOrganisation;
                    case "09":
                        return Filter(digitPattern, lstOrganisation);
                    case "ae":
                        return Filter(aePattern, lstOrganisation);
                    case "fj":
                        return Filter(fjPattern, lstOrganisation);
                    case "kn":
                        return Filter(knPattern, lstOrganisation);
                    case "or":
                        return Filter(orPattern, lstOrganisation);
                    case "sv":
                        return Filter(svPattern, lstOrganisation);
                    case "wz":
                        return Filter(wzPattern, lstOrganisation);
                }
            return null;
        }

        /// <summary>
        /// Inserts the specified facility.
        /// </summary>
        /// <param name="facility">The facility.</param>
        /// <returns></returns>
        public bool Insert(Facility facility)
        {

            return FacilityDao.Instance.Insert(facility);



        }

        /// <summary>
        /// Updates the specified facility.
        /// </summary>
        /// <param name="facility">The facility.</param>
        /// <returns></returns>
        public bool Update(Facility facility)
        {
            if (facility == null) return false;

            var result = FacilityDao.Instance.Update(facility);
            return result;

        }

        /// <summary>
        /// Gets the facility by id.
        /// </summary>
        /// <param name="facilityId">The facility id.</param>
        /// <returns></returns>
        public Facility GetFacilityById(Guid facilityId)
        {
            return FacilityDao.Instance.GetById(facilityId);
        }

        /// <summary>
        /// Gets the type of the facility.
        /// </summary>
        /// <returns></returns>
        public Collection<ReferenceData> GetFacilityType()
        {

            return ReferenceDataDataAccess.Instance.GetReferenceData(100);
        }
    }
}
